import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getCompanyID, getLoadMapList, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { getManageTabSwitch, getSearchList } from '../mconfig';
const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地
// const baseUrl = 'wenchuang';
// const baseUrls = 'KLapi';

const warnManageService = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //工单类型
  ];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  /** 获取搜索参数 */
  const searchListParams = getSearchList(loadMdicMapObj?.loadMapInfo, queryTypeObj);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        // current: 1,
        // pageSize: 10000,
        // sort: 'readTime',
        // order: 'desc',
        // dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };

      // const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
      //   // dicTransName: 'orderType',
      //   // orgTransName: `${ISearchTranslateAll.OWNER_DEPT}`,
      //   // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      // });
      const requestURL = `${baseUrl}/assetStore/treeList/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request?.readTime) {
      const [readTimeStart, readTimeEnd] = request.readTime;
      request['startTime'] = readTimeStart.format('YYYY-MM-DD 00:00:00');
      request['endTime'] = readTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['readTime'];
    }

    delete request['createFormNameDept'];
    delete request['createFormNameUser'];
    if (request?.createrID && request?.createrID?.length > 0) {
      request['createrID'] = request?.createrID?.toString();
    } else {
      delete request['createrID'];
    }

    run({
      ...request,
    });
  };

  /** 搜索列表  */
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};

// 保存接口
const wareaddService = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (reqParams: any) => {
      let PostParams = {
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        storeState: 1,
        ...reqParams,
      };
      /**处理一下省市县*/
      let reqUrl = `${baseUrl}/assetStore/add`;
      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
      onSuccess: Callback,
    },
  );

  return {
    wareaddRun: request.run,
    taskPushLoading: request.loading,
    taskPushPagination: request.pagination,
  };
};

// 修改
const wareditService = (Callback: (res: Record<string, any>) => void) => {
  const request = useRequest(
    (reqParams: any) => {
      let PostParams = {
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
        storeState: 1,
        ...reqParams,
      };
      /**处理一下省市县*/
      let reqUrl = `${baseUrl}/assetStore/edit`;
      /***发起请求数据接口**/
      return requestFunComponFiex('PUT', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
      onSuccess: Callback,
    },
  );

  return {
    wareeditRun: request.run,
    taskPushLoading: request.loading,
    taskPushPagination: request.pagination,
  };
};

const warcloseService = (params) => {
  const obj = {
    id: params.id,
  };
  const requestURL = `${baseUrl}/assetStore/close`;
  return requestFunComponFiex('PUT', requestURL, obj);
};

const waropenService = (params) => {
  const obj = {
    id: params.id,
    pId: params.pId,
  };
  const requestURL = `${baseUrl}/assetStore/open`;
  return requestFunComponFiex('PUT', requestURL, obj);
};

export { warnManageService, wareaddService, wareditService, warcloseService, waropenService };
